package com.hddf.project.base;

import java.io.File;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.kommii.log.LogNames;

import com.hddf.project.pojos.User;
import com.hddf.project.service.LogsService;
import com.hddf.project.util.HttpUtil;
import com.hddf.project.util.JsonProvider;
import com.hddf.project.util.O;
import com.hddf.project.util.Stct;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 父类BaseAction
 * 
 * @author xxz
 */
@SuppressWarnings({ "serial" })
public abstract class BaseAction extends ActionSupport {

	public String state = "success";
	public Object msg = "操作成功";
	public Map<String, Object> map = new HashMap<String, Object>();
	public BaseService baseService = null;
	public Integer lengthData = Stct.LENGTH_DATA;// 显示的数据长度
	public Integer page;
	public LogsService ls;
	public User user;

	public List<?> lto;
	public O o = new O();
	// 页数,总页数,总条数,每页所显示的条数
	public int pager = 1, count, cotse, pcnum = 10;
	public Integer bxswn = 0;

	// oper:j(跳页操作),oper:o(执行操作);
	public String oper = "", ts = "";

	public final String JUMP = "j", SEUE = "o", T = "T", E = "E";

	// 文件
	public File file = null;
	public String fileContentType = new String(), fileFileName = new String(); // 文件类型,文件名称

	// 日志
	// private Log logger = LogFactory.getLog(BaseAction.class);

	/** 操作成功调用路径,返回 success,异常信息调用路径，返回error */
	protected String successPath, errorPath;

	/** 输入路径,返回input */
	protected String inputPath;

	/** 重定向路径，返回redirect */
	protected String redirectPath;

	/** 重定向Action路径,返回redirectAction */
	protected String redirectActionPath;

	protected Log log = LogFactory.getLog(getClass());

	private BaseDaoImpl baseDaoImpl;

	public String formatDouble(double s) {
		DecimalFormat fmt;
		if (bxswn == 2) {
			fmt = new DecimalFormat("##0.00");
		} else if (bxswn == 1) {
			fmt = new DecimalFormat("##0.0");
		} else {
			fmt = new DecimalFormat("##0");
		}
		return fmt.format(s);
	}

	/**
	 * 获取总页数：根据每页显示的数据量算出总页数
	 * 
	 * @param size
	 * @return
	 */
	public int getPageCount(int size) {
		int maxPage = 0;
		if (size > 0) {
			int lengthData = Stct.LENGTH_DATA;// 每页显示的数据长度
			if (lengthData > size) {
				maxPage = 1;
			} else {
				int yu = size % lengthData;
				if (yu > 0) {
					maxPage = 1 + Integer.valueOf(size / lengthData);
				} else {
					maxPage = Integer.valueOf(size / lengthData);
				}
			}
		}
		return maxPage;
	}

	/**
	 * List去除重复值
	 * 
	 * @param srtList
	 * @return
	 */
	public List<String> removeDuplicate(List<String> srtList) {
		List<String> tempList = new ArrayList<String>();
		for (String str : srtList) {
			if (!tempList.contains(str)) {
				tempList.add(str);
			}
		}
		return tempList;
	}

	public static String unescape(String src) {
		if (src == null)
			return null;
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < src.length()) {
			pos = src.indexOf("%", lastPos);
			if (pos == lastPos) {
				if (src.charAt(pos + 1) == 'u') {
					ch = (char) Integer.parseInt(src.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				} else if (src.charAt(pos + 1) == ' ' || src.charAt(pos + 1) == ';') {
					tmp.append(src.substring(pos, pos + 1));
					lastPos = pos + 1;
				} else {
					ch = (char) Integer.parseInt(src.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			} else {
				if (pos == -1) {
					tmp.append(src.substring(lastPos));
					lastPos = src.length();
				} else {
					tmp.append(src.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		return tmp.toString();
	}

	/**
	 * 添加日志
	 * 
	 * @param type
	 *            1：类别管理；2：类别查询；3：项目管理；4：类别查询；
	 */
	public void log(int type, String doContent, int id, String name) {
		String logName = "";
		switch (type) {
		case 1:
			logName = LogNames.category_manager;
			break;
		case 2:
			logName = LogNames.category_select;
			break;
		case 3:
			logName = LogNames.project_manager;
			break;
		case 4:
			logName = LogNames.project_select;
			break;
		case 5:
			logName = LogNames.plan_manager;
			break;
		case 6:
			logName = LogNames.plan_select;
			break;
		}
		getRequest().setAttribute("logname", logName);
		getRequest().setAttribute("logs", doContent + " id: " + id + " ," + doContent + " name: " + name);
	}

	/**
	 * 添加日志
	 * 
	 * @param type
	 *            1：类别管理；2：类别查询；3：项目管理；4：类别查询；
	 */
	public void log(int type, String doContent, int id) {
		String logName = "";
		switch (type) {
		case 1:
			logName = LogNames.category_manager;
			break;
		case 2:
			logName = LogNames.category_select;
			break;
		case 3:
			logName = LogNames.project_manager;
			break;
		case 4:
			logName = LogNames.project_select;
			break;
		case 5:
			logName = LogNames.plan_manager;
			break;
		case 6:
			logName = LogNames.plan_select;
			break;
		}
		getRequest().setAttribute("logname", logName);
		getRequest().setAttribute("logs", doContent + ":" + id);
	}

	/**
	 * 添加日志
	 * 
	 * @param type
	 *            1：类别管理；2：类别查询；3：项目管理；4：类别查询；
	 */
	public void log(int type, String doContent) {
		String logName = "";
		switch (type) {
		case 1:
			logName = LogNames.category_manager;
			break;
		case 2:
			logName = LogNames.category_select;
			break;
		case 3:
			logName = LogNames.project_manager;
			break;
		case 4:
			logName = LogNames.project_select;
			break;
		case 5:
			logName = LogNames.plan_manager;
			break;
		case 6:
			logName = LogNames.plan_select;
			break;
		}
		getRequest().setAttribute("logname", logName);
		getRequest().setAttribute("logs", doContent);
	}

	/**
	 * 添加日志
	 * 
	 * @param type
	 *            1：类别管理；2：类别查询；3：项目管理；4：类别查询；
	 */
	public void planLog(int id, String ids) {
		getRequest().setAttribute("logname", LogNames.plan_manager);
		getRequest().setAttribute("logs", "bulk add project_plan id: " + id + " ,bulk add project_plan_item ids: " + ids);
	}

	/**
	 * 输出json
	 * 
	 * @param map
	 * @return
	 */
	public static String getJson(Map<String, Object> map) {
		return JsonProvider.getJson(map);
	}

	public HttpServletResponse getResponse() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Pragma", "No-Cache");
		response.setHeader("Cache-Control", "No-Cache");
		response.setDateHeader("Expires", 0);
		return response;
	}

	public HttpServletRequest getRequest() {
		HttpServletRequest request = ServletActionContext.getRequest();
		return request;
	}

	public HttpSession getSession() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		return session;
	}

	protected static InputStream getResourceAsStream(String path) {
		return HttpUtil.getResourceAsStream(path);
	}

	protected ServletContext getServletContext() {
		return HttpUtil.getServletContext();
	}

	public String getSuccessPath() {
		return successPath;
	}

	public void setSuccessPath(String successPath) {
		this.successPath = successPath;
	}

	public String getErrorPath() {
		return errorPath;
	}

	public void setErrorPath(String errorPath) {
		this.errorPath = errorPath;
	}

	public String getInputPath() {
		return inputPath;
	}

	public void setInputPath(String inputPath) {
		this.inputPath = inputPath;
	}

	public String getRedirectPath() {
		return redirectPath;
	}

	public void setRedirectPath(String redirectPath) {
		this.redirectPath = redirectPath;
	}

	public String getRedirectActionPath() {
		return redirectActionPath;
	}

	public void setRedirectActionPath(String redirectActionPath) {
		this.redirectActionPath = redirectActionPath;
	}

	public Object getMsg() {
		return msg;
	}

	public void setMsg(Object msg) {
		this.msg = msg;
	}

	public Log getLog() {
		return log;
	}

	public void setLog(Log log) {
		this.log = log;
	}

	/**
	 * request
	 * 
	 * @return
	 */
	protected HttpServletRequest getHttpServletRequest() {
		return HttpUtil.getHttpServletRequest();
	}

	/**
	 * response
	 * 
	 * @return
	 */
	protected HttpServletResponse getHttpServletResponse() {
		return HttpUtil.getHttpServletResponse();
	}

	/**
	 * ActionName
	 * 
	 * @return
	 */
	protected String getActionName() {
		return HttpUtil.getActionName();
	}

	/**
	 * ajax
	 * 
	 * @param obj
	 */
	protected void printHttpServletResponse(Object obj) {
		HttpUtil.printHttpServletResponse(obj);
	}

	/**
	 * session存值
	 */
	protected void setSession(Object key, Object value) {
		HttpUtil.setSession(key, value);
	}

	/**
	 * session取值
	 */
	protected Object getSession(Object key) {
		return HttpUtil.getSession(key);
	}

	/**
	 * 删除session中一个
	 */
	public void removeSession(Object key) {
		HttpUtil.removeSession(key);
	}

	/**
	 * 清空session
	 */
	public void removeAllSession() {
		HttpUtil.removeAllSession();
	}

	/**
	 * @return the baseDaoImpl
	 */
	public BaseDaoImpl getBaseDaoImpl() {
		return baseDaoImpl;
	}

	/**
	 * @param baseDaoImpl
	 *            the baseDaoImpl to set
	 */
	@Resource
	public void setBaseDaoImpl(BaseDaoImpl baseDaoImpl) {
		this.baseDaoImpl = baseDaoImpl;
	}

	public int getPager() {
		return pager;
	}

	public void setPager(int pager) {
		this.pager = pager;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCotse() {
		return cotse;
	}

	public void setCotse(int cotse) {
		this.cotse = cotse;
	}

	public int getPcnum() {
		return pcnum;
	}

	public void setPcnum(int pcnum) {
		this.pcnum = pcnum;
	}

	public List<?> getLto() {
		return lto;
	}

	public void setLto(List<?> lto) {
		this.lto = lto;
	}

	public O getO() {
		return o;
	}

	public void setO(O o) {
		this.o = o;
	}

	public Integer getBxswn() {
		return bxswn;
	}

	public void setBxswn(Integer bxswn) {
		this.bxswn = bxswn;
	}

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public String getTs() {
		return ts;
	}

	public void setTs(String ts) {
		this.ts = ts;
	}

	public String getJUMP() {
		return JUMP;
	}

	public String getSEUE() {
		return SEUE;
	}

	public String getT() {
		return T;
	}

	public String getE() {
		return E;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	public BaseService getBaseService() {
		return baseService;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public int getLengthData() {
		return lengthData;
	}

	public void setLengthData(int lengthData) {
		this.lengthData = lengthData;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public LogsService getLs() {
		return ls;
	}

	public void setLs(LogsService ls) {
		this.ls = ls;
	}

	public void setLengthData(Integer lengthData) {
		this.lengthData = lengthData;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
